import {_decorator, Color, color, Component, Label, Node, Quat, quat, Sprite, tween, Tween, v3, Vec3} from 'cc';
import {NativeEvt} from "./GameConfig";
import {MoneyAnim} from "./MoneyAnim";
import {GameMain} from "./GameMain";
import {AudioClipID, AudioManager} from "./AudioManager";
import Platform from "./Platform";

const { ccclass, property } = _decorator;

@ccclass('ResultUI')
export class ResultUI extends Component {

    @property({type:MoneyAnim})
    moneyAnim:MoneyAnim=null;

    public targetNode:Node=null;
    public inited:boolean=false;
    public pageClosed:boolean=false;

    private spinNode:Node=null;
    private spinAwardLabel:Label=null;

    private awardCurVal:number=30;
    private awardTargetVal:number=0;
    private awardNumLabel:Label=null;

    onLoad()
    {
        this.init();
        Platform.createInsertAd();
    }

    public init()
    {
        if(this.inited)
            return;
        this.targetNode=this.node.getChildByName("window");
        let awardTimesNode=this.targetNode.getChildByName("awardTimesBar");
        this.spinNode=awardTimesNode.getChildByName("spin");

        let spinAwardBtnNode:Node=this.targetNode.getChildByName("spinAwardBtn");
        this.spinAwardLabel=spinAwardBtnNode.getChildByName("Label").getComponent(Label);

        let awardNode:Node=this.targetNode.getChildByName("award");
        this.awardNumLabel=awardNode.getChildByName("add").getComponent(Label);

        this.inited=true;
    }

    public startMoveSpin()
    {
        this.spinNode.setPosition(-251, -40, 0);
        tween(this.spinNode).to(1.5, { position: v3(251, -40, 0)},{ easing: 'cubicInOut' }).to(1.5, { position: v3(-251, -40, 0)},{ easing: 'cubicInOut' }).union().repeatForever().start();
    }

    public stopMoveSpin()
    {
        Tween.stopAllByTarget(this.spinNode);
    }


    public onGiveupClicked()
    {
        if(this.pageClosed)
            return;
        this.pageClosed=true;
        this.stopMoveSpin();

        this.moneyAnim.setDstNode(GameMain.instance.mainUI.moneyNode);
        this.moneyAnim.play(()=>{

        });

        //获取金币
        GameMain.instance.gameData.addMoney(this.awardCurVal);
        GameMain.instance.mainUI.updateMoneyLabel();

        this.scheduleOnce(()=>{
            AudioManager.playClip(AudioClipID.Coin);
        }, 0.3);

        this.scheduleOnce(()=>{
            this.close();
        }, 1);
    }

    public onSpinAwardClicked()
    {
        if(this.pageClosed)
            return;

        this.stopMoveSpin();
        GameMain.instance.setVedioAwardScene(NativeEvt.VedioResultAward);
        Platform.playVideoAd(() =>
        {
            this.videoAward();
        },()=>{
            this.startMoveSpin();
        });
    }

    public videoAward()
    {
        let spinVal:number=this.getSpinVal();

        this.moneyAnim.setDstNode(GameMain.instance.mainUI.moneyNode);
        this.moneyAnim.play(()=>{

        });

        this.scheduleOnce(()=>{
            AudioManager.playClip(AudioClipID.Coin);
        }, 0.3);

        this.awardTargetVal=spinVal * this.awardCurVal;
        GameMain.instance.gameData.addMoney(this.awardTargetVal);
        GameMain.instance.mainUI.updateMoneyLabel();

        this.scheduleOnce(()=>{
            this.close();
        }, 1.2);
    }

    private getSpinVal():number
    {
        let x:Vec3=this.spinNode.getPosition().x;
        if((x>=-255 && x<-191) || (x<=255 && x>191))
        {
            return 2;
        }
        else if((x>=-191 && x<-127) || (x<=191 && x>127))
        {
            return 3;
        }
        else if((x>=-127 && x<-47) || (x<=124 && x>47))
        {
            return 4;
        }
        else if(x>=-47 && x<=47)
        {
            return 5;
        }
        return 5;
    }

    public show()
    {
        if(!this.inited)
            this.init();
        if(this.node.active)
            return;

        this.pageClosed=false;

        if(Platform.isOV())
        {
            Platform.createOVCustomAd();
        }

        this.node.active=true;
        this.targetNode.setScale(0.3, 0.3,1);

        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .start();

        this.startMoveSpin();
    }

    public close()
    {
        //this.node.active=false;
        this.stopMoveSpin();

        tween(this.targetNode).to(0.2,{scale:v3(0.5, 0.5, 0.5)}).call(()=>{
            this.node.active=false;
            GameMain.instance.backToStartUI();
        }).start();
    }

    update(dt:number)
    {
        let spinVal:number=this.getSpinVal();
        this.spinAwardLabel.string="奖励 x"+spinVal;

        if(!this.pageClosed)
            this.awardNumLabel.string="+" + this.awardCurVal*spinVal;
        else
            this.awardNumLabel.string="+" + this.awardCurVal;

        /*
        if(this.awardTargetVal>0 && this.awardCurVal<this.awardTargetVal)
        {
            //let addVal:number=Math.floor(dt * 50);
            this.awardCurVal+=5;
            if(this.awardCurVal>this.awardTargetVal)
                this.awardCurVal=this.awardTargetVal;
            this.awardNumLabel.string="+" + this.awardCurVal;
        }*/
    }
}

